package com.dengc.service.impl;

import com.dengc.mapper.SysUserMapper;
import com.dengc.pojo.SysUser;
import com.dengc.service.SysUserService;
import com.dengc.utils.Contants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser login(String account, String password) {
        SysUser sysUser = new SysUser();
        sysUser.setAccount(account);
        sysUser.setPassword(password);
        List<SysUser> list = sysUserMapper.getSysUserByCondition(sysUser);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    //根据id修改
    @Override
    public int updateSysUser(SysUser sysUser) {
        return sysUserMapper.updateSysUser(sysUser);
    }

    //分页功能实现
    @Override
    public PageInfo<SysUser> getSysUserByPage(SysUser sysUser, int pageNum) {
        //开启分页
        PageHelper.startPage(pageNum, Contants.PAGESIZE);
        //分页好的数据保存起来
        List<SysUser> list = sysUserMapper.getSysUserByCondition(sysUser);
        return new PageInfo<>(list);
   }

   //根据id查询用户信息实现
    @Override
    public SysUser getIdSysUser(Integer id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        List<SysUser> list = sysUserMapper.getSysUserByCondition(sysUser);
        return list.get(0);
    }

    //添加用户信息实现
    @Override
    public int addUser(SysUser sysUser) {
        return sysUserMapper.addUser(sysUser);
    }

    //删除用户信息实现
    @Override
    public int deleteUser(Integer id) {
        return sysUserMapper.deleteUser(id);
    }

    @Override
    public int getCountByRoleId(Integer roleid) {
        SysUser sysUser = new SysUser();
        sysUser.setRoleId(roleid);
        List<SysUser> list = sysUserMapper.getSysUserByCondition(sysUser);
        return list.size();
    }
}
